package com.ittony.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ittony.constant.StatusConstant;
import com.ittony.context.BaseContext;
import com.ittony.dto.CategoryDTO;
import com.ittony.dto.CategoryPageQueryDTO;
import com.ittony.entity.Category;
import com.ittony.entity.Employee;
import com.ittony.mapper.CategoryMapper;
import com.ittony.result.PageResult;
import com.ittony.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public void addCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);

       /* category.setStatus(StatusConstant.ENABLE);
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUserId(BaseContext.getCurrentId());
        category.setUpdateUserId(BaseContext.getCurrentId());*/

        categoryMapper.addCategory(category);
    }

    @Override
    public PageResult page(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());

        Page<Category> page =  categoryMapper.page(categoryPageQueryDTO);

        long total = page.getTotal();
        List<Category> records = page.getResult();

        return new PageResult(total,records);
    }

    @Override
    public void deleteCategoryById(Long id) {
        categoryMapper.deleteCategoryById(id);
    }

    @Override
    public void updateCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);

        /*category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUserId(BaseContext.getCurrentId());*/

        categoryMapper.updateCategory(category);
    }

    @Override
    public void enableDisableCategory(Integer status,Long id) {
        Category category = Category.builder().id(id).status(status).build();
        categoryMapper.updateCategory(category);
    }

    @Override
    public List<Category> listCategoryByType(Integer type) {
        return categoryMapper.listCategoryByType(type);
    }
}
